{extend name="../../../view/public/base" /}

{block name="css"}
<style>
    body {
        background-color: #F2F2F2;
    }
</style>
{/block}

{block name="body"}
<div class="bw-container">
    <div class="bw-main">
    <div class="layui-row layui-col-space15">
        <div class="layui-col-md12">

            <!--搜索区域-->
            <div class="layui-card">
                <div class="layui-collapse" lay-accordion="" lay-filter="search">
                    <div class="layui-colla-item">
                        <h2 class="layui-colla-title">查询条件</h2>
                        <div class="layui-colla-content">
                            <form id="searchForm" class="layui-form" lay-filter="searchForm" autocomplete="off">

                                <div class="layui-form-item layui-inline">
                                    <label class="layui-form-label">管理员ID</label>
                                    <div class="layui-input-inline">
                                        <input type="number" name="id" placeholder="请输入管理员ID"
                                               class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-form-item">
                                    <div class="layui-input-block">
                                        <button class="layui-btn layui-btn-normal" type="button" lay-submit lay-filter="searchBtn">
                                            搜索
                                        </button>
                                        <button class="layui-btn layui-btn-primary" type="button" onclick="resetForm()">
                                            重置
                                        </button>
                                    </div>
                                </div>
                            </form>
                        </div>
                    </div>
                </div>
            </div>

            <!--列表区域-->
            <div class="layui-card" style="background-color: #F2F2F2;">
                <table id="list" lay-filter="listFilter"></table>

                <!--工具栏-->
                <script type="text/html" id="listToolbar">
                    <a class="layui-btn layui-btn-sm layui-bg-black" lay-event="refresh" title="刷新"><i class="layui-icon layui-icon-refresh" style="font-size: 8px !important;"></i> </a>
                    {Auth:node name='/bwmall/admin.BwmallShopProductReply/add'}
                    <a class="layui-btn layui-btn-sm layui-btn-normal" lay-event="add" title="添加账号">添加管理员</a>
                    {/Auth:node}
                    {Auth:node name='/bwmall/admin.BwmallShopProductReply/del'}
                    <a class="layui-btn layui-btn-sm layui-btn-danger" lay-event="del" title="删除">删除</a>
                    {/Auth:node}
                </script>
                <script type="text/html" id="auth_group_access">
                    {{#  layui.each(d.auth_group_access, function(index, item){ }}
                    <span class="layui-badge layui-bg-orange">{{ item}}</span>
                    {{#  }); }}
                </script>
                <!--订单ID-->
                <script type="text/html" id="status">
                    {{#  if(d.my != true){ }}
                    <input type="checkbox"  name="status" value="{{d.id}}"  lay-skin="switch" lay-text="正常|封禁" lay-filter="status" {{ d.status == 1 ? 'checked' : '' }}>
                    {{#  } }}
                    {{#  if(d.my == true){ }}
                    {{#  if(d.status == 1){ }}<span class="layui-badge" style="background-color: #5FB878">正常</span> {{#  } }}
                    {{#  if(d.status != 1){ }}<span class="layui-badge">封禁</span> {{#  } }}
                    {{#  } }}
                </script>
                <script type="text/html" id="memberId">
                   {{d._name}}
                   {{#  if(d.is_top == true){ }}
                   <span class="layui-badge ">顶级租户</span>
                   {{# } else { }}
                   <span class="layui-badge layui-bg-blue">子租户</span>
                   {{#  } }}
                   {{#  if(d.my == true){ }}
                   <span class="layui-badge layui-bg-orange">当前租户</span>
                   {{#  } }}
                </script>
                <!--状态开关-->
                <script type="text/html" id="listBar">
                    {{#  if(d.my != true){ }}
                    <a class="layui-btn layui-btn-xs layui-btn-normal" lay-event="edit">修改用户信息</a>
                    <a class="layui-btn layui-btn-xs layui-btn-danger" lay-event="del">删除</a>
                    {{#  } }}

                </script>
            </div>
        </div>
    </div>
    </div>
</div>

{/block}

{block name="js"}
<script>
    var element, layer, form, table, bwajax, manage,xmSelect,bwajax,$,eleTree,treetable;

    layui.use(['element', 'layer', 'form', 'table', 'bwajax', 'manage', 'bwajax'], function () {
        element = layui.element;
        layer = layui.layer;
        form = layui.form;
        table = layui.table;
        manage = layui.manage;
        bwajax = layui.bwajax.instance();
        $ = layui.jquery;

        //执行列表
        table.render({
            elem: '#list',
            url: "{:Url('user')}",
            response: {
                statusCode: 200,  //规定成功的状态码，默认：0
            },
            parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.data.data.total, //解析数据长度
                    "data": res.data.data.list //解析数据列表
                };
            },
            toolbar: '#listToolbar',
            cols: [[
                {type:'checkbox', fixed: 'left'},
                {field: 'id', title: '管理员ID',   fixed: 'left',  templet: '#memberId'},
                {field: 'username', title: '管理员账号', align: 'center'},
                {field: 'nickname', title: '管理员名称', align: 'center' },
                {field: 'auth_group_access', title: '拥有角色组', align: 'center', templet: '#auth_group_access'},
                {field: 'status', title: '管理员状态', align: 'center',  templet: '#status'},
                {title: '操作', toolbar: '#listBar', align: 'center', fixed: "right", width: 200}
            ]],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true
        });


        isClick = true;
        form.on('switch(status)', function(obj){
            tipText = "操作成功";
            var emelment = $('.layui-form-switch');

            if(isClick){
                isClick = false;

                bwajax.post("{:Url('setStatus')}",{id:this.value})
                    .then(function (response) {
                        if (response.data.data.errcode === 0) {
                            layer.msg(tipText, {icon: 1});
                        } else {
                            reload();
                            layer.msg(response.data.msg, {icon: 2});
                        }

                        // for (var i = 0; i < emelment.length; i++) {
                        //     $(emelment[i]).addClass('layui-disabled');
                        //     $(emelment[i]).unbind();
                        // }
                        // setTimeout(function(){
                        isClick = true;
                        //     curr = $('.layui-laypage-em').next().text();
                        //     location.reload();
                        // },1500);
                    });
            }else{
                layer.msg('请不要操作太频繁！', {
                    icon: 5,
                    time: 1000
                },function() {
                    reload();
                });
            }
        });

        //监听表格头部工具栏事件
        table.on('toolbar(listFilter)', function (obj) {
            var checkStatus = table.checkStatus(obj.config.id);
            switch (obj.event) {
                case 'refresh':
                    reload();
                    break;
                case 'add':
                    layer.open({
                        title: '新增',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('add')}",
                    });
                    break;
                case 'del':
                    var checkStatus = table.checkStatus('list');
                    var data = checkStatus.data;

                    var idsArr = [];
                    data.forEach(function(value) {
                        idsArr.push(value.id);
                    });
                    ids = idsArr.join(',');

                    if(!ids) layer.msg('没有选中行', {icon: 2});
                    else del(ids);

                    break;

            }
        });

        //监听工具条
        table.on('tool(listFilter)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）

            switch (layEvent) {
                case 'edit':
                    layer.open({
                        title: '编辑',
                        type: 2,
                        shade: 0.2,
                        maxmin: true,
                        area: ['50%', '90%'],
                        content: "{:Url('edit')}?id=" + data.id,
                    });
                    break;
                case 'del':
                    del(obj.data.id);
                    break;

            }
        });

        form.on('submit(searchBtn)', function (data) {
            reload(1);

            return false;
        })


    });

    //删除
    function del(ids) {
        layer.confirm('确认删除?', function (index) {
            //ajax调用后台接口
            bwajax.post("{:Url('delete')}?ids=" + ids)
                .then(function (response) {
                    if (response.data.data.errcode === 0) {
                        layer.msg('删除成功', {icon: 1});
                        reload();
                    } else {
                        layer.msg(response.data.msg, {icon: 2});
                    }
                });
            layer.close(index);
        });
    }
</script>
{/block}